<%@ page import="net.pws.oos.security.SecurityUtils"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../../header.jsp"%>

<div class="border bg">
<div class="page-header">
	<h2>地区列表</h2>
	<div class="cms_toolbar">
		<% if(SecurityUtils.allowedResource("/sm/area/create")){ %>
		<a id="_create_item_action" href="province.do" class="btn"><i class="icon-plus"></i>新建</a>
		<%} %>
	</div>
</div>

<div>
	<br />
	<table id="listTable" class="table table-striped cms-table">
		<thead>
			<tr>
				<th width="30">&nbsp;</th>
				<th>名称</th>
				<th width="120">操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

</div>

</div>

<table id="listTemplate" class="hide">
  	<tr>
  		<td></td>
	    <td><a href=""></a></td>
	    <td><a href="">下级地区</a> <a href="">删除</a></td>
  	</tr>
</table>

<script type="text/javascript">
function list(){
	$("#listTable tbody").empty();
	block("#listTable", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/dict/area/list.json",
    	dataType:"json",
    	success:function(json) {
    		unblock("#listTable");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
			buildList(json.data);
    	},
		error: function(message) {
			unblock("#listTable");
			$.alert(message);
		}
	});
}

function buildList(data){
	if(data.length == 0){
		return;
	}
	$(data).each(function(index, item) {
		buildTd(item, index).appendTo($("#listTable").find("tbody"));
	});
}

function buildTd(item, index){
	var taskEle = $("#listTemplate").clone();
	taskEle.find("tr").attr("id", "item" + item.id);
	var tdList = taskEle.find("td");
	$(tdList.get(0)).text(index + 1);
	$(tdList.get(1)).find("a").attr("href", ctx + "/biz/dict/area/province.do?id=" + item.id);
	$(tdList.get(1)).find("a").text(item.name);
	var optList = $(tdList.get(2)).find("a");
	$(optList.get(0)).attr("href", ctx + "/biz/dict/area/cities.do?parentId=" + item.id);
	$(optList.get(1)).attr("href", "javascript:deleteData('" + item.id + "')");
	
	return taskEle.find("tr");
}

function deleteData(id) {
	if (!confirm("你确定删除此地区吗？")){
		return;
	}
	block("#listTable", "处理中...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/dict/area/delete.json",
    	data: {id : id},
    	dataType:"json",
    	success:function(json) {
    		unblock("#listTable");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
			list();
    	},
		error: function(message) {
			unblock("#listTable");
			$.alert(message);
		}
	});
}

$(document).ready(function() {
	list();
});
</script>

<%@ include file="../../../footer.jsp"%>